/**
 * @file GLInfo.cpp
 * @ingroup Utilities
 *
 * @brief GLInfo implementation
 */

/* --
 * Copyright (c) 2012, Christophe Huriaux
 * Distributed under the terms of the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * --
 */

#include <utils/GLInfo.h>
#include <GL/gl.h>
#include <debug/logger.h>

/**
 * @brief OpenGL call to retrieve a system string
 *
 *    @c GLInfo::getString() is a private helper used by others members of
 * @c GLInfo used to retrieve a system string from the OpenGL API.
 *
 * @param name ID of the string to retrieve through @c glGetString.
 * @return The queried string
 * @retval "???" This undefined string is returned upon error
 */
std::string
GLInfo::getString(unsigned int name)
{
	const GLubyte* str = ::glGetString(name);

	if (str == NULL)
	{
		flogk(debug) << (void*)str << " - " << glGetError() << std::endl;
		return std::string("???");
	}

	return std::string((const char*)str);
}


/**
 * @brief OpenGL call to retrieve a system integer
 *
 *    @c GLInfo::getInteger() is a private helper used by others members of
 * @c GLInfo used to retrieve a system integer value from the OpenGL API.
 *
 * @param name ID of the integer value to retrieve through @c glGetIntegerv.
 * @return The queried integer value
 */
int
GLInfo::getInteger(unsigned int name)
{
	int result;

	glGetIntegerv(name, &result);
	return result;
}


/**
 * @brief OpenGL call to retrieve a system float
 *
 *    @c GLInfo::getInteger() is a private helper used by others members of
 * @c GLInfo used to retrieve a system floating point value from the OpenGL
 * API.
 *
 * @param name ID of the integer value to retrieve through @c glGetFloatv.
 * @return The queried floating point value
 */
float
GLInfo::getFloat(unsigned int name)
{
	float result;

	glGetFloatv(name, &result);
	return result;
}


/**
 * @brief Returns the vendor of the current OpenGL implementation
 *
 * @return A string describing the OpenGL implementation vendor
 */
std::string
GLInfo::vendor()
{
	return GLInfo::getString(GL_VENDOR);
}


/**
 * @brief Returns the renderer of the current OpenGL implementation
 *
 * @return A string describing the OpenGL implementation renderer
 */
std::string
GLInfo::renderer()
{
	return GLInfo::getString(GL_RENDERER);
}


/**
 * @brief Returns the version of the current OpenGL implementation
 *
 * @return A string describing the OpenGL implementation version
 */
std::string
GLInfo::version()
{
	return GLInfo::getString(GL_VERSION);
}


/**
 * @brief Returns the vendor of the current Shaders Script Language
 *  version
 *
 * @return A string describing the Shaders Script Language version in use
 */
std::string
GLInfo::shslVersion()
{
	return GLInfo::getString(GL_SHADING_LANGUAGE_VERSION);
}


/**
 * @brief Returns a list of the supported extensions
 *
 * @return A comma-separated list of the OpenGL extensions supported by the
 * actual implementation
 */
std::string
GLInfo::extensions()
{
	return GLInfo::getString(GL_EXTENSIONS);
}


/**
 * @brief Returns the maximum number of possible vertex attributes
 *
 * @return The maximum number of defineable attributes for a given vertex
 */
int
GLInfo::maxVertexAttribs()
{
	return GLInfo::getInteger(GL_MAX_VERTEX_ATTRIBS);
}


/**
 * @brief Returns the number of texture units
 *
 * @return The number of texture units of the current implementation
 */
int
GLInfo::maxTextureUnits()
{
	return GLInfo::getInteger(GL_MAX_TEXTURE_UNITS_ARB);
}
